#include "Node.h"


CNode::CNode(void)
{
}


CNode::~CNode(void)
{
}

CNode::CNode(RECT rect)
{
	TL = TR = BL = BR = NULL;
	Content = new list<CNode*>();
	this->Area = rect;
	this->Height = Area.bottom - Area.top;
	this->Width = Area.right - Area.left;
	this->listObj = new list<CObjects*>();
}

void CNode::Insert(CObjects* obj)
{
	if (Content->empty())// node la
	{
		//them tai node nay
		listObj->push_back(obj);
		IsFull = listObj->size() > _Max;
		bool minSizeNotReached = (this->Width > _MinWidth && this->Height > _MinHeight);
		if(IsFull && minSizeNotReached)
		{
			CreateChildNodes();
			list<CObjects*>::iterator i,j;
			i = listObj->begin();
			j = listObj->end();
			while(i != j)
			{
				if(Distribute((*i)) == true)//them vao node con
				{
					i = listObj->erase(i);
				}
				else
					i++;
			}

		}
	}
	else
	{
		if(Distribute(obj) == false)//ko co node con nao chua ca
		{
			listObj->push_back(obj);
		}
	}
}

//chia node
bool CNode::Distribute(CObjects* obj)
{
	if (IsBound(obj->RectArea,this->TL->Area))
	{
		this->TL->Insert(obj);
		return true;
	}
	if (IsBound(obj->RectArea,this->TR->Area))
	{
		this->TR->Insert(obj);
		return true;
	}
	if (IsBound(obj->RectArea,this->BL->Area))
	{
		this->BL->Insert(obj);
		return true;
	}
	if(IsBound(obj->RectArea,this->BR->Area))
	{
		this->BR->Insert(obj);
		return true;
	}
	return false;
}

bool CNode::IsBound(RECT nho,RECT lon)
{
	//nho nam trong lon
	if(nho.left >= lon.left && nho.top >= lon.top && nho.right <=lon.right && nho.bottom <= lon.bottom)
	{
		return true;
	}
	return false;

}

void CNode::CreateChildNodes()
{
	RECT rect;
	int h,w;
	h = this->Height/2;
	w = this->Width/2;
	//node Top left
	rect.left = this->Area.left;
	rect.right = this->Area.left + w;
	rect.top = this->Area.top;
	rect.bottom = this->Area.top + h;
	this->TL = new CNode(rect);
	//node Top right
	rect.left = this->Area.right - w;
	rect.right = this->Area.right;
	this->TR = new CNode(rect);
	//node Bottom left
	rect.left = this->Area.left;
	rect.right = this->Area.left + w;
	rect.top = this->Area.bottom - h;
	rect.bottom = this->Area.bottom;
	this->BL = new CNode(rect);
	//node Bottom right
	rect.left = this->Area.right - w;
	rect.right = this->Area.right;
	this->BR = new CNode(rect);
	this->Content->push_back(this->TL);
	this->Content->push_back(this->TR);
	this->Content->push_back(this->BL);
	this->Content->push_back(this->BR);
}

void CNode::Draw(RECT Screen)
{
	//list<CObjects*>::iterator begin,end;
	//begin = this->listObj->begin();
	//end = this->listObj->end();
	//while(begin != end)
	//{
	//	//if(IntersectRect((*begin).Rect,Screen))
	//	//{

	//	//}
	//}
}

void CNode::GetObjectInScreen(list<CObjects*> *_list, RECT Screen)
{
	//get obj tai node
	list<CObjects*>::iterator begin,end;
	begin = this->listObj->begin();
	end = this->listObj->end();
	while(begin != end)
	{
		if(IntersectRect((*begin)->RectArea,Screen))
		{	
			_list->push_back(*begin);
			begin = listObj->erase(begin);
		}
		else
			begin++;
	}

	if(Content->empty() == false)
	{
		//tim node con chua Screen
		if( IsBound(Screen,this->TL->Area))
		{
			this->TL->GetObjectInScreen(_list,Screen);
			return;
		}
		if( IsBound(Screen,this->TR->Area))
		{
			this->TR->GetObjectInScreen(_list,Screen);
			return;
		}
		if( IsBound(Screen,this->BL->Area))
		{
			this->BL->GetObjectInScreen(_list,Screen);
			return;
		}
		if( IsBound(Screen,this->BR->Area))
		{
			this->BR->GetObjectInScreen(_list,Screen);
			return;
		}
		//tim node con chua cac phan nho cua screen
		if (IntersectRect(Screen,this->TL->Area))
		{
			RECT r = Overlap(Screen,this->TL->Area);
			this->TL->GetObjectInScreen(_list,r);
		}
		if (IntersectRect(Screen,this->TR->Area))
		{
			RECT r = Overlap(Screen,this->TR->Area);
			this->TR->GetObjectInScreen(_list,r);
		}
		if (IntersectRect(Screen,this->BL->Area))
		{
			RECT r = Overlap(Screen,this->BL->Area);
			this->BL->GetObjectInScreen(_list,r);
		}
		if (IntersectRect(Screen,this->BR->Area))
		{
			RECT r = Overlap(Screen,this->BR->Area);
			this->BR->GetObjectInScreen(_list,r);
		}
	}
}

bool CNode::IntersectRect(RECT rect1,RECT rect2)
{
	//rect1 tren rect2
	if(rect1.bottom <= rect2.top + 1)
		return false;
	//rect1 duoi rect2
	//if(rect1.top + 7 >= rect2.bottom - 1 )//rect1.top + 10 >= rect2.bottom - 1
	if(rect1.top >= rect2.bottom - 1 )
		return false;
	//rect1 trai rect2
	if(rect1.right - 5 <= rect2.left +1)//rect1.right - 5 <= rect2.left +1
		return false;
	//rect1 phai rect2
	if(rect1.left + 5  >= rect2.right - 1)//rect1.left  + 5 >= rect2.right - 1
		return false;
	return true;
}

bool CNode::Intersect100(RECT rect1,RECT rect2)
{
	//rect1 tren rect2
	if(rect1.bottom <= rect2.top)
		return false;
	//rect1 duoi rect2
	if(rect1.top >= rect2.bottom)//rect1.top + 10 >= rect2.bottom - 1
		return false;
	//rect1 trai rect2
	if(rect1.right <= rect2.left)//rect1.right - 5 <= rect2.left +1
		return false;
	//rect1 phai rect2
	if(rect1.left >= rect2.right)//rect1.left  + 5 >= rect2.right - 1
		return false;
	return true;
}


bool CNode::IntersectRectEnemy(RECT rect1,RECT rect2)
{
	//rect1 tren rect2
	if(rect1.bottom <= rect2.top + 1)
		return false;
	//rect1 duoi rect2
	if(rect1.top + 5 >= rect2.bottom - 1 )
		return false;
	//rect1 trai rect2
	if(rect1.right - 5 <= rect2.left +1)
		return false;
	//rect1 phai rect2
	if(rect1.left  + 5 >= rect2.right - 1)
		return false;
	return true;
}

RECT CNode::Overlap(RECT A,RECT B)
{
	RECT _overlap;
	_overlap.left = (B.left > A.left)? B.left:A.left;
	_overlap.right = (B.right < A.right) ? B.right : A.right;
	_overlap.top = (B.top > A.top) ? B.top : A.top;
	_overlap.bottom = (B.bottom < A.bottom)? B.bottom : A.bottom;
	return _overlap;
}

bool CNode::CheckCollision(RECT RectObj)
{
	//get obj tai node
	list<CObjects*>::iterator begin,end;
	begin = this->listObj->begin();
	end = this->listObj->end();
	while(begin != end)
	{
		if((*begin)->Die == false && (*begin)->iKind !=2 && IntersectRect(RectObj,(*begin)->RectArea))
		{	
			return true;
		}
		begin++;
	}

	if(Content->empty() == false)
	{
		//tim node con chua Screen
		if( IsBound(RectObj,this->TL->Area))
		{
			return this->TL->CheckCollision(RectObj);

		}
		if( IsBound(RectObj,this->TR->Area))
		{
			return this->TR->CheckCollision(RectObj);

		}
		if( IsBound(RectObj,this->BL->Area))
		{
			return this->BL->CheckCollision(RectObj);

		}
		if( IsBound(RectObj,this->BR->Area))
		{
			return this->BR->CheckCollision(RectObj);

		}
		//tim node con chua cac phan nho cua screen
		if (IntersectRect(RectObj,this->TL->Area))
		{
			RECT r = Overlap(RectObj,this->TL->Area);
			//return this->TL->CheckCollision(RectObj);
			if (this->TL->CheckCollision(r))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->TR->Area))
		{
			RECT r = Overlap(RectObj,this->TR->Area);
			//return this->TR->CheckCollision(RectObj);
			if (this->TR->CheckCollision(r))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->BL->Area))
		{
			RECT r = Overlap(RectObj,this->BL->Area);
			//return this->BL->CheckCollision(RectObj);
			if (this->BL->CheckCollision(r))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->BR->Area))
		{
			RECT r = Overlap(RectObj,this->BR->Area);
			//return this->BR->CheckCollision(RectObj);
			if (this->BR->CheckCollision(r))
			{
				return true;
			}
		}
	}
	return false;
}

bool CNode::CheckListCollision(RECT RectObj,list<CObjects*> *_ListCollision,bool &_KQ)
{
	//get obj tai node
	list<CObjects*>::iterator begin,end;
	begin = this->listObj->begin();
	end = this->listObj->end();
	while(begin != end)
	{
		if((*begin)->Die == false && (*begin)->iKind !=2 && IntersectRect(RectObj,(*begin)->RectArea))
		{	
			//return true;
			if (((*begin)->iKind == 4 || (*begin)->iKind == 5 || (*begin)->iKind == 1 || (*begin)->iKind == 3))//bonus and item and enemy and marks
			{
				// add vao list
				_ListCollision->push_back((*begin));
			}
			else
			{
				_KQ = true;
			}
		}
		begin++;
	}

	if(Content->empty() == false)
	{
		//tim node con chua Screen
		if( IsBound(RectObj,this->TL->Area))
		{
			return this->TL->CheckListCollision(RectObj,_ListCollision,_KQ);

		}
		if( IsBound(RectObj,this->TR->Area))
		{
			return this->TR->CheckListCollision(RectObj,_ListCollision,_KQ);

		}
		if( IsBound(RectObj,this->BL->Area))
		{
			return this->BL->CheckListCollision(RectObj,_ListCollision,_KQ);

		}
		if( IsBound(RectObj,this->BR->Area))
		{
			return this->BR->CheckListCollision(RectObj,_ListCollision,_KQ);

		}
		//tim node con chua cac phan nho cua screen
		if (IntersectRect(RectObj,this->TL->Area))
		{
			RECT r = Overlap(RectObj,this->TL->Area);
			//return this->TL->CheckCollision(RectObj);
			if (this->TL->CheckListCollision(r,_ListCollision,_KQ))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->TR->Area))
		{
			RECT r = Overlap(RectObj,this->TR->Area);
			//return this->TR->CheckCollision(RectObj);
			if (this->TR->CheckListCollision(r,_ListCollision,_KQ))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->BL->Area))
		{
			RECT r = Overlap(RectObj,this->BL->Area);
			//return this->BL->CheckCollision(RectObj);
			if (this->BL->CheckListCollision(r,_ListCollision,_KQ))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->BR->Area))
		{
			RECT r = Overlap(RectObj,this->BR->Area);
			//return this->BR->CheckCollision(RectObj);
			if (this->BR->CheckListCollision(r,_ListCollision,_KQ))
			{
				return true;
			}
		}
	}
	return false;
}


bool CNode::CheckCollisionNoE(RECT RectObj)
{
	//get obj tai node
	list<CObjects*>::iterator begin,end;
	begin = this->listObj->begin();
	end = this->listObj->end();
	while(begin != end)
	{
		if((*begin)->Die == false && (*begin)->iKind !=2 && Intersect100(RectObj,(*begin)->RectArea))
		{	
			//return true;
			if (((*begin)->iKind == 4 || (*begin)->iKind == 5 || (*begin)->iKind == 1 || (*begin)->iKind == 3))//bonus and item and enemy and marks
			{

			}
			else
			{
				return  true;
			}
		}
		begin++;
	}

	if(Content->empty() == false)
	{
		//tim node con chua Screen
		if( IsBound(RectObj,this->TL->Area))
		{
			return this->TL->CheckCollisionNoE(RectObj);

		}
		if( IsBound(RectObj,this->TR->Area))
		{
			return this->TR->CheckCollisionNoE(RectObj);

		}
		if( IsBound(RectObj,this->BL->Area))
		{
			return this->BL->CheckCollisionNoE(RectObj);

		}
		if( IsBound(RectObj,this->BR->Area))
		{
			return this->BR->CheckCollisionNoE(RectObj);

		}
		//tim node con chua cac phan nho cua screen
		if (IntersectRect(RectObj,this->TL->Area))
		{
			RECT r = Overlap(RectObj,this->TL->Area);
			//return this->TL->CheckCollision(RectObj);
			if (this->TL->CheckCollisionNoE(r))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->TR->Area))
		{
			RECT r = Overlap(RectObj,this->TR->Area);
			//return this->TR->CheckCollision(RectObj);
			if (this->TR->CheckCollisionNoE(r))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->BL->Area))
		{
			RECT r = Overlap(RectObj,this->BL->Area);
			//return this->BL->CheckCollision(RectObj);
			if (this->BL->CheckCollisionNoE(r))
			{
				return true;
			}
		}
		if (IntersectRect(RectObj,this->BR->Area))
		{
			RECT r = Overlap(RectObj,this->BR->Area);
			//return this->BR->CheckCollision(RectObj);
			if (this->BR->CheckCollisionNoE(r))
			{
				return true;
			}
		}
	}
	return false;
}

//void CNode::GetListOfList(list<list<CObjects*>*> *BigList)
//{
//	if (Content->empty())// node la
//	{
//		BigList->push_back(listObj);
//		return;
//	}
//	else
//	{
//		BigList->push_back(listObj);
//		this->TL->GetListOfList(BigList);
//		this->TR->GetListOfList(BigList);
//		this->BL->GetListOfList(BigList);
//		this->BR->GetListOfList(BigList);
//	}
//}

void CNode::DrawGame(list<CObjects*> *_list, RECT Screen)
{
	//get obj tai node
	list<CObjects*>::iterator begin,end;
	begin = this->listObj->begin();
	end = this->listObj->end();
	while(begin != end)
	{
		if(Intersect100((*begin)->RectArea,Screen))
		{	

			/*	_list->push_back(*begin);*/
			if ((*begin)->iKind == 4 || (*begin)->iKind == 5)//bonus anh item
			{
				//delete bonus, item bi Player an
				CDynamicObjects *a = (CDynamicObjects*)(*begin);
				if(a->Die == true)//chet
				{
					begin = listObj->erase(begin);
				}
				else
				{
					_list->push_back(*begin);
					begin++;
				}
			}
			else
			{
				_list->push_back(*begin);
				begin++;
			}
		}
		else
		{
			begin++;
		}

	}

	if(Content->empty() == false)
	{
		//tim node con chua Screen
		if( IsBound(Screen,this->TL->Area))
		{
			this->TL->DrawGame(_list,Screen);
			return;
		}
		if( IsBound(Screen,this->TR->Area))
		{
			this->TR->DrawGame(_list,Screen);
			return;
		}
		if( IsBound(Screen,this->BL->Area))
		{
			this->BL->DrawGame(_list,Screen);
			return;
		}
		if( IsBound(Screen,this->BR->Area))
		{
			this->BR->DrawGame(_list,Screen);
			return;
		}
		//tim node con chua cac phan nho cua screen
		if (IntersectRect(Screen,this->TL->Area))
		{
			RECT r = Overlap(Screen,this->TL->Area);
			this->TL->DrawGame(_list,r);
		}
		if (IntersectRect(Screen,this->TR->Area))
		{
			RECT r = Overlap(Screen,this->TR->Area);
			this->TR->DrawGame(_list,r);
		}
		if (IntersectRect(Screen,this->BL->Area))
		{
			RECT r = Overlap(Screen,this->BL->Area);
			this->BL->DrawGame(_list,r);
		}
		if (IntersectRect(Screen,this->BR->Area))
		{
			RECT r = Overlap(Screen,this->BR->Area);
			this->BR->DrawGame(_list,r);
		}
	}
}

void CNode::Move(RECT Screen,CNode *_NodeRoot)
{
	//tim tung obj thuoc Screen, move obj

	list<CObjects*>::iterator begin,end;
	begin = this->listObj->begin();
	int n = listObj->size();
	Delta = G_DeltaTime;
	D3DXVECTOR3 Oldpos;
	for(int i = 0;i<n;++i)
	{
		if((*begin)->iKind == 1 && (*begin)->iType == 0)//obj co the move ( Enemy-goomby)
		{
			CEnemy *G_Goomba = new CEnemy();
			CDynamicObjects *obj = (CDynamicObjects*)(*begin);
			if (obj->Die == false)//chua chet
			{
				Oldpos= obj->Pos;
				G_Goomba->Animation(obj);
				begin = listObj->erase(begin);//obj tiep theo
				if (G_Goomba->CheckMoveY(obj,Delta,_NodeRoot) == true)// Ko di chuyen y
				{
					if(G_Goomba->CheckMoveX(obj,Delta,_NodeRoot)==true)//Ko di chuyen  X
					{
						//doi huong du chuyen
						obj->left = !obj->left;
						obj->Pos = Oldpos;
						obj->RectArea.left = Oldpos.x;
						obj->RectArea.right = Oldpos.x + 50;
						obj->RectArea.top = Oldpos.y;
						obj->RectArea.bottom = Oldpos.y + 50;
						obj->RectArea.top += 10;
						listObj->push_back(obj);
					}
				}
			}
			else//chet
			{
				G_Goomba->Animation(obj);
				//if(timeGetTime() >= obj->_LastTimeOut + obj->_TimeOut)
				//{
				//	begin = listObj->erase(begin);//obj tiep theo
				//}
				//else
				//{
					begin++;
				//}
			}

		}
	//	else if((*begin)->iKind == 1 && (*begin)->iType == 3)// Khunglong
	//	{
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		if (obj->_Die == false)//chua chet
	//		{
	//			Oldpos= obj->_Pos;
	//			Mario->_KhungLong->Animation(obj);
	//			begin = listObj->erase(begin);//obj tiep theo
	//			if (Mario->_KhungLong->CheckMoveY(obj,_Delta,_NodeRoot) == true)// Ko di chuyen y
	//			{
	//				if(Mario->_KhungLong->CheckMoveX(obj,_Delta,_NodeRoot)==true)//Ko di chuyen  X
	//				{
	//					//doi huong du chuyen
	//					obj->_Left = !obj->_Left;
	//					obj->_Pos = Oldpos;
	//					obj->RectArea.left = Oldpos.x;
	//					obj->RectArea.right = Oldpos.x + 100;
	//					obj->RectArea.top = Oldpos.y + Mario->_KhungLong->HeightNho;
	//					obj->RectArea.bottom = Oldpos.y + 100;
	//					listObj->push_back(obj);
	//				}
	//			}
	//		}
	//		else//da chet
	//		{
	//			Mario->_KhungLong->Animation(obj);
	//			if(timeGetTime() >= obj->_LastTimeOut + obj->_TimeOut)
	//			{
	//				begin = listObj->erase(begin);//obj tiep theo
	//			}
	//			else
	//			{
	//				begin++;
	//			}
	//		}
	//	}
	//	else if((*begin)->iKind == 1 && (*begin)->iType == 4)// conrua
	//	{
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		if (obj->_Die == false)//chua chet
	//		{
	//			Oldpos= obj->_Pos;
	//			Mario->_ConRua->Animation(obj);
	//			begin = listObj->erase(begin);//obj tiep theo
	//			if (obj->_State == 2)//up la di chuyen
	//			{
	//				if (Mario->_ConRua->CheckMoveY2(obj,_Delta,_NodeRoot) == true)// Ko di chuyen y
	//				{
	//					if(Mario->_ConRua->CheckMoveX2(obj,_Delta,_NodeRoot)==true)//Ko di chuyen  X
	//					{
	//						//doi huong du chuyen
	//						obj->_Left = !obj->_Left;
	//						obj->_Pos = Oldpos;
	//						obj->RectArea.left = Oldpos.x;
	//						obj->RectArea.right = Oldpos.x + 50;
	//						obj->RectArea.top = Oldpos.y;
	//						obj->RectArea.bottom = Oldpos.y + 100;
	//						obj->RectArea.top += 10;
	//						listObj->push_back(obj);
	//					}
	//				}
	//			}
	//			else//trang thai khac
	//			{
	//				if (Mario->_ConRua->CheckMoveY(obj,_Delta,_NodeRoot) == true)// Ko di chuyen y
	//				{
	//					if(Mario->_ConRua->CheckMoveX(obj,_Delta,_NodeRoot)==true)//Ko di chuyen  X
	//					{
	//						//doi huong du chuyen
	//						obj->_Left = !obj->_Left;
	//						obj->_Pos = Oldpos;
	//						obj->RectArea.left = Oldpos.x;
	//						obj->RectArea.right = Oldpos.x + 50;
	//						obj->RectArea.top = Oldpos.y;
	//						obj->RectArea.bottom = Oldpos.y + 100;
	//						obj->RectArea.top += 10;
	//						listObj->push_back(obj);
	//					}
	//				}
	//			}
	//		}
	//		else//da chet xoa
	//		{
	//			begin = listObj->erase(begin);//obj tiep theo
	//		}
	//	}

	//	else if((*begin)->iKind == 1 && (*begin)->iType == 1)// eato
	//	{			
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		if (obj->_Die == false)//chua chet
	//		{
	//			Oldpos= obj->_Pos;
	//			Mario->_Eato->Animation(obj);
	//			begin = listObj->erase(begin);//obj tiep theo
	//			++obj->_LastMove;
	//			if(obj->_LastMove>= obj->_TimeOut)
	//			{
	//				if(obj->_LastMove >= obj->_TimeOut + 30)
	//				{
	//					obj->_TimeOut = obj->_LastMove + 70;
	//					obj->_Left = !obj->_Left;
	//				}
	//				else
	//				{
	//					Mario->_Eato->MoveY(obj,_Delta);
	//				}
	//			}
	//			listObj->push_back(obj);
	//		}
	//		else//da chet xoa
	//		{
	//			begin = listObj->erase(begin);//obj tiep theo
	//		}



	//	}
	//	else if((*begin)->iKind == 1 && (*begin)->iType == 2)// Num nho
	//	{			
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		if (obj->_Die == false)//chua chet
	//		{
	//			Mario->_Other->Animation(obj);	
	//			begin++;//obj tiep theo
	//		}
	//		else//chet
	//		{
	//			begin = listObj->erase(begin);//obj tiep theo
	//		}
	//	}
	//	else if((*begin)->iKind == 1 && (*begin)->iType == 6)// bullet of gun
	//	{			
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		if (obj->_Die == false)//chua chet
	//		{
	//			begin = listObj->erase(begin);//obj tiep theo
	//			Mario->_BulletE->CheckMoveX(obj,_Delta,_NodeRoot);	
	//		}
	//		else//chet
	//		{
	//			begin = listObj->erase(begin);//obj tiep theo
	//		}
	//	}
	//	else if((*begin)->iKind == 1 && (*begin)->iType == 5)// gun
	//	{			
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		if (timeGetTime() >= obj->_TimeOut + 4000)//shoot
	//		{
	//			Mario->_Audio->Play("Load");
	//			obj->_TimeOut = timeGetTime();
	//			CDynamicObjects* a;
	//			if (obj->_Pos.x - Mario->_PosScreen.x <= 350)//shoot ben phai
	//			{
	//				D3DXVECTOR3 pos = obj->_Pos;
	//				pos.y += 10;
	//				pos.x += 100;
	//				RECT r;
	//				r.left = pos.x;
	//				r.right = pos.x + 50;
	//				r.top = pos.y;
	//				r.bottom = pos.y + 30;
	//				a = new CDynamicObjects(1,6,pos,r,0,0,0);
	//				a->_Left = false;
	//			}
	//			else//ban ben trai
	//			{
	//				D3DXVECTOR3 pos = obj->_Pos;
	//				pos.y += 10;
	//				pos.x -= 50;
	//				RECT r;
	//				r.left = pos.x;
	//				r.right = pos.x + 50;
	//				r.top = pos.y;
	//				r.bottom = pos.y + 30;
	//				a = new CDynamicObjects(1,6,pos,r,0,0,0);
	//				a->_Left = true;
	//			}
	//			_NodeRoot->Insert(a);
	//		}
	//		begin++;//obj tiep theo
	//	}

	//	else if ((*begin)->iKind == 4)
	//	{
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		Mario->_Bonus->Update(obj);
	//		//------------------------ moi them vao
	//		if(obj->iType == 4 && obj->_TimeOut != 0)//di chuyen gach
	//		{
	//			begin = listObj->erase(begin);//obj tiep theo
	//			++obj->_LastMove;
	//			if(obj->_LastMove>= obj->_TimeOut)
	//			{
	//				if(obj->_LastMove <= 10)
	//				{
	//					Mario->_Bonus->MoveY(obj,_Delta);
	//					if(obj->_LastMove == 10)
	//					{
	//						obj->_Left = false;
	//					}
	//				}
	//				else if (obj->_LastMove <= 19)
	//				{
	//					Mario->_Bonus->MoveY(obj,_Delta);
	//					if(obj->_LastMove == 19)
	//					{
	//						obj->_Pos.y = obj->_State;//loi ding thuoc tinh _state de dua gach ve cho cu
	//						//set rect lai trang thai cu
	//						obj->RectArea.top = obj->_State;
	//						obj->RectArea.bottom = obj->_State + 50;
	//						obj->_Left = true;
	//						obj->_TimeOut = 0;
	//						obj->_LastMove = 0;
	//					}
	//				}
	//			}
	//			listObj->push_back(obj);
	//		}
	//		else
	//		{
	//			//---------------------------ket thuc moi themvao
	//			begin++;//obj tiep theo
	//		}

	//	}
	//	else if ((*begin)->iKind == 5 && (*begin)->iType != 2)//item
	//	{
	//		CDynamicObjects *obj = (CDynamicObjects*)(*begin);
	//		Oldpos= obj->_Pos;
	//		begin = listObj->erase(begin);//obj tiep theo
	//		if (Mario->_Item->CheckMoveY(obj,_Delta,_NodeRoot) == true)// Ko di chuyen y
	//		{
	//			if(Mario->_Item->CheckMoveX(obj,_Delta,_NodeRoot)==true)//Ko di chuyen  X
	//			{
	//				//doi huong du chuyen
	//				obj->_Left = !obj->_Left;
	//				obj->_Pos = Oldpos;
	//				obj->RectArea.left = Oldpos.x;
	//				obj->RectArea.right = Oldpos.x + 50;
	//				obj->RectArea.top = Oldpos.y;
	//				obj->RectArea.bottom = Oldpos.y + 50;
	//				listObj->push_back(obj);
	//			}
	//		}
	//	}
	//	else
	//	{
	//		begin++;//obj tiep theo
	//	}
else begin++;
	}

	if(Content->empty() == false)
	{
		//tim node con chua Screen
		if( IsBound(Screen,this->TL->Area))
		{
			this->TL->Move(Screen,_NodeRoot);
			return;
		}
		if( IsBound(Screen,this->TR->Area))
		{
			this->TR->Move(Screen,_NodeRoot);
			return;
		}
		if( IsBound(Screen,this->BL->Area))
		{
			this->BL->Move(Screen,_NodeRoot);
			return;
		}
		if( IsBound(Screen,this->BR->Area))
		{
			this->BR->Move(Screen,_NodeRoot);
			return;
		}
		//tim node con chua cac phan nho cua screen
		if (IntersectRect(Screen,this->TL->Area))
		{
			RECT r = Overlap(Screen,this->TL->Area);
			this->TL->Move(r,_NodeRoot);
		}
		if (IntersectRect(Screen,this->TR->Area))
		{
			RECT r = Overlap(Screen,this->TR->Area);
			this->TR->Move(r,_NodeRoot);
		}
		if (IntersectRect(Screen,this->BL->Area))
		{
			RECT r = Overlap(Screen,this->BL->Area);
			this->BL->Move(r,_NodeRoot);
		}
		if (IntersectRect(Screen,this->BR->Area))
		{
			RECT r = Overlap(Screen,this->BR->Area);
			this->BR->Move(r,_NodeRoot);
		}
	}
}